package com.isoft.airportsystem.controller.system;

import com.isoft.airportsystem.common.Result;
import com.isoft.airportsystem.entity.system.User;
import com.isoft.airportsystem.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页控制器
 */
@RestController
@RequestMapping("/api/system/home")
public class HomeController {

    @Autowired
    private UserService userService;

    /**
     * 获取首页配置
     *
     * @param userId 用户ID
     * @return 首页配置
     */
    @GetMapping("/config")
    public Result<Map<String, Object>> getHomeConfig(@RequestParam Long userId) {
        // 获取用户信息
        User user = userService.getById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }

        // 获取用户权限
        List<String> permissions = userService.selectPermissionsByUserId(userId);

        // 构建返回结果
        Map<String, Object> data = new HashMap<>();

        // 设置用户基本信息
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId", user.getUserId());
        userInfo.put("username", user.getUsername());
        userInfo.put("realName", user.getRealName());
        userInfo.put("avatar", user.getAvatar());
        data.put("userInfo", userInfo);

        // 设置首页快捷入口
        List<Map<String, Object>> quickEntries = new ArrayList<>();

        // 基础快捷入口（所有用户都可见）
        Map<String, Object> flightSearch = new HashMap<>();
        flightSearch.put("id", 1);
        flightSearch.put("title", "航班查询");
        flightSearch.put("icon", "search");
        flightSearch.put("path", "/flight/search");
        quickEntries.add(flightSearch);

        // 根据权限添加不同的快捷入口
        if (permissions.contains("system:flight:manage")) {
            Map<String, Object> flightManage = new HashMap<>();
            flightManage.put("id", 2);
            flightManage.put("title", "航班管理");
            flightManage.put("icon", "plane");
            flightManage.put("path", "/flight/manage");
            quickEntries.add(flightManage);
        }

        if (permissions.contains("system:baggage:manage")) {
            Map<String, Object> baggageManage = new HashMap<>();
            baggageManage.put("id", 3);
            baggageManage.put("title", "行李管理");
            baggageManage.put("icon", "luggage");
            baggageManage.put("path", "/baggage/manage");
            quickEntries.add(baggageManage);
        }

        if (permissions.contains("system:admin")) {
            Map<String, Object> userManage = new HashMap<>();
            userManage.put("id", 4);
            userManage.put("title", "用户管理");
            userManage.put("icon", "user");
            userManage.put("path", "/system/user");
            quickEntries.add(userManage);

            Map<String, Object> roleManage = new HashMap<>();
            roleManage.put("id", 5);
            roleManage.put("title", "角色管理");
            roleManage.put("icon", "peoples");
            roleManage.put("path", "/system/role");
            quickEntries.add(roleManage);

            Map<String, Object> permissionManage = new HashMap<>();
            permissionManage.put("id", 6);
            permissionManage.put("title", "权限管理");
            permissionManage.put("icon", "tree-table");
            permissionManage.put("path", "/system/permission");
            quickEntries.add(permissionManage);
        }

        data.put("quickEntries", quickEntries);

        // 设置首页展示的统计卡片
        List<Map<String, Object>> statCards = new ArrayList<>();

        // 基础统计卡片（所有用户都可见）
        Map<String, Object> totalFlights = new HashMap<>();
        totalFlights.put("id", 1);
        totalFlights.put("title", "总航班数");
        totalFlights.put("value", 1250);  // 模拟数据，实际应从数据库查询
        totalFlights.put("icon", "plane");
        totalFlights.put("color", "#409EFF");
        statCards.add(totalFlights);

        Map<String, Object> todayFlights = new HashMap<>();
        todayFlights.put("id", 2);
        todayFlights.put("title", "今日航班");
        todayFlights.put("value", 86);    // 模拟数据，实际应从数据库查询
        todayFlights.put("icon", "calendar");
        todayFlights.put("color", "#67C23A");
        statCards.add(todayFlights);

        // 根据权限添加不同的统计卡片
        if (permissions.contains("system:flight:manage")) {
            Map<String, Object> delayedFlights = new HashMap<>();
            delayedFlights.put("id", 3);
            delayedFlights.put("title", "延误航班");
            delayedFlights.put("value", 12);    // 模拟数据，实际应从数据库查询
            delayedFlights.put("icon", "time");
            delayedFlights.put("color", "#E6A23C");
            statCards.add(delayedFlights);

            Map<String, Object> canceledFlights = new HashMap<>();
            canceledFlights.put("id", 4);
            canceledFlights.put("title", "取消航班");
            canceledFlights.put("value", 3);     // 模拟数据，实际应从数据库查询
            canceledFlights.put("icon", "close");
            canceledFlights.put("color", "#F56C6C");
            statCards.add(canceledFlights);
        }

        if (permissions.contains("system:admin")) {
            Map<String, Object> totalUsers = new HashMap<>();
            totalUsers.put("id", 5);
            totalUsers.put("title", "系统用户");
            totalUsers.put("value", 56);      // 模拟数据，实际应从数据库查询
            totalUsers.put("icon", "user");
            totalUsers.put("color", "#909399");
            statCards.add(totalUsers);
        }

        data.put("statCards", statCards);

        return Result.success(data);
    }

    /**
     * 获取系统公告
     *
     * @return 系统公告列表
     */
    @GetMapping("/notices")
    public Result<List<Map<String, Object>>> getNotices() {
        // 构建返回结果
        List<Map<String, Object>> notices = new ArrayList<>();

        // 模拟数据，实际应从数据库查询
        Map<String, Object> notice1 = new HashMap<>();
        notice1.put("id", 1);
        notice1.put("title", "系统升级通知");
        notice1.put("content", "系统将于2023年6月15日凌晨2:00-4:00进行升级维护，请提前做好相关工作安排。");
        notice1.put("type", "通知");
        notice1.put("createTime", "2023-06-10 10:00:00");
        notices.add(notice1);

        Map<String, Object> notice2 = new HashMap<>();
        notice2.put("id", 2);
        notice2.put("title", "新功能上线");
        notice2.put("content", "航班变更申请功能已上线，请各位用户及时了解使用方法。");
        notice2.put("type", "公告");
        notice2.put("createTime", "2023-06-08 14:30:00");
        notices.add(notice2);

        Map<String, Object> notice3 = new HashMap<>();
        notice3.put("id", 3);
        notice3.put("title", "安全提醒");
        notice3.put("content", "请各位用户定期修改密码，确保账号安全。");
        notice3.put("type", "提醒");
        notice3.put("createTime", "2023-06-05 09:15:00");
        notices.add(notice3);

        return Result.success(notices);
    }
}